package andreiR;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.testng.*;
import org.testng.annotations.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class AddNewMovie extends andreiR.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();
  private WebElement username;
  private WebElement password;
  private WebElement submit;
  private WebElement addmove;
  private WebElement savebutton;
  private WebElement menuhome;
  private WebElement imdbsearch;
  private WebElement sbmove;
  private WebElement link;
  private WebElement savemove;
  private String move = "My First Movie";
 
   
  
  @Test
  public void testUntitled2() throws Exception { driver.get(baseUrl + "/php4dvd/");
  //TODO: add findElement to method  
  this.username = driver.findElement(By.id("username"));
  this.password = driver.findElement(By.name("password"));
  this.submit = driver.findElement(By.name("submit"));
  login("admin","admin");
  this.addmove = driver.findElement(By.cssSelector("img[alt=\"Add movie\"]"));
  addMovie();  
  this.imdbsearch= driver.findElement(By.id("imdbsearch"));  
  this.sbmove = driver.findElement(By.cssSelector("input[type=\"submit\"]"));
  Serch(move);
  this.link = driver.findElement(By.linkText(this.move));   //TODO: add variable to search
  link.click();
  this.savemove=driver.findElement(By.cssSelector("img[alt=\"Save\"]"));
  Save();
  /*
  	driver.findElement(By.id("username")).sendKeys("admin");
  	driver.findElement(By.name("password")).clear();
  	driver.findElement(By.name("password")).sendKeys("admin");  	
  	driver.findElement(By.name("submit")).click();  	
    driver.findElement(By.cssSelector("img[alt=\"Add movie\"]")).click();    
    driver.findElement(By.id("imdbsearch")).clear();
    driver.findElement(By.id("imdbsearch")).sendKeys("My First movie");   
    driver.findElement(By.cssSelector("input[type=\"submit\"]")).click();    
    driver.findElement(By.linkText("My First Movie")).click();    
    driver.findElement(By.cssSelector("img[alt=\"Save\"]")).click();
    */
    driver.findElement(By.xpath("//*[@id='wrapper']/header/div/nav/ul/li[1]/a[@href='http://localhost/php4dvd/']")).click();
  }
  
  public void login(String u, String p){
	  username.clear();
	  username.sendKeys(u);
	  password.clear();
	  password.sendKeys(p);
	  submit.click();    
  }
  public void addMovie(){
	  addmove.click(); 
  }
  
  public void Serch(String s){
	  imdbsearch.clear(); 
	  imdbsearch.sendKeys(s);
	  sbmove.click();
  }
  public void  Save(){ 	 
	  savemove.click();
  } 
   
  
//*[@id="wrapper"]/header/div/nav/ul/li[1]/a
  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
  @AfterTest
  public void quitDriver() {
	  driver.quit();	  
  }
}